package com.flyco.modules.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyco.modules.sms.entity.SmsDiscountStore;
import com.flyco.modules.sms.mapper.SmsDiscountStoreMapper;
import com.flyco.modules.sms.model.SmsDiscountModel;
import com.flyco.modules.sms.service.ISmsDiscountStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 指定门店券
 * @Author: flyco
 * @Date:   2020-09-28
 * @Version: V1.0
 */
@Service
public class SmsDiscountStoreServiceImpl extends ServiceImpl<SmsDiscountStoreMapper, SmsDiscountStore> implements ISmsDiscountStoreService {

    @Autowired
    private SmsDiscountStoreMapper smsDiscountStoreMapper;

    @Override
    public boolean deleteByMainId(Long mainId) {
        return smsDiscountStoreMapper.deleteByMainId(mainId);
    }

    @Override
    public List<SmsDiscountStore> selectByMainId(Long mainId) {
        return smsDiscountStoreMapper.selectByMainId(mainId);
    }

    @Override
    public void addDiscountStore(SmsDiscountModel smsDiscountModel) {
        List<SmsDiscountStore> discountStores = new ArrayList<>();
        for (Long storeId : smsDiscountModel.getStoreIdList()){
            SmsDiscountStore discountStore = new SmsDiscountStore();
            discountStore.setDiscountId(smsDiscountModel.getId());
            discountStore.setStoreId(storeId);
            discountStores.add(discountStore);
        }
        this.saveBatch(discountStores);
    }

    @Override
    public void editDiscountStore(SmsDiscountModel smsDiscountModel) {
        LambdaQueryWrapper<SmsDiscountStore> discountStoreQuery = new LambdaQueryWrapper<>();
        discountStoreQuery.eq(SmsDiscountStore::getDiscountId,smsDiscountModel.getId());
        this.remove(discountStoreQuery);
        this.addDiscountStore(smsDiscountModel);
    }

}
